<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 新增试题按钮 -->
        <div class="btn_wrapper">
          <el-button type="success" icon="el-icon-edit" @click="$router.push('/questions/new')">新增试题</el-button>
        </div>

        <!-- 搜索列表 -->
        <el-form ref="formData" :model="formData">
          <el-row type="flex">
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select v-model="formData.subjectID" placeholder="请选择学科" @change="changeSubject" @focus="list" >
                  <el-option  v-for="item in subjectList" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="试题类型">
                <el-select v-model="formData.questionType" placeholder="请选择试题类型">
                  <el-option v-for="(item, index) in questionType" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="题目备注">
                <el-input v-model="formData.remarks" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select v-model="formData.catalogID" placeholder="请选择二级目录">
                  <el-option  v-for="item in SecondaryDirectorys" :key="item.id" :label="item.directoryName" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="难度">
                <el-select v-model="formData.difficulty" placeholder="请选择难度">
                  <el-option v-for="(item, index) in difficulty" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="企业简称">
                <el-input v-model="formData.shortName" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select v-model="formData.tags" placeholder="请选择标签">
                  <el-option v-for="item in tagsList" :key="item.id"  :label="item.tagName" value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="方向">
                <el-select v-model="formData.direction" placeholder="请选择方向">
                  <el-option v-for="(item,index) in direction" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="城市" class="inp">
                <el-select
                  v-model="formData.province"
                  placeholder="请选择"
                  style="width: 48%; margin-right: 4%;"
                  @change="handleProvince"
                  @focus="getCityData"
                >
                  <el-option
                    v-for="(item,index) in citySelect.province"
                    :key="index"
                    :label="item"
                    :value="item" />
                </el-select>
                <el-select
                  v-model="formData.city"
                  placeholder="请选择"
                  style="width: 48%"
                >
                  <el-option
                    v-for="(item, index) in citySelect.cityDate"
                    :key="index"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input v-model="formData.keyword" placeholder="根据题干搜索" />
              </el-form-item>
              <el-form-item label="录入人">
                <el-select v-model="formData.creatorID" placeholder="请选择录入人" @focus="userList">
                  <el-option v-for="item in usersList" :key="item.id" :label="item.username" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item class="btn_wrapper">
                <el-button size="small" @click="resetForm">清除</el-button>
                <el-button size="small" type="primary" @click="searchChoice">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <!-- 选项卡 -->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部" name="null" />
          <el-tab-pane label="待审核" name="0" />
          <el-tab-pane label="已审核" name="1" />
          <el-tab-pane label="已拒绝" name="2" />
        </el-tabs>

        <el-alert
          type="info"
          :closable="false"
          show-icon
          style="margin-bottom: 15px;"
        >
          <template #title>
            数据一共 {{ total }} 条
          </template>
        </el-alert>

        <!-- 精选题库列表  -->
        <el-table
          :data="questionsData"
          style="width: 100%">
          <el-table-column
            prop="number"
            label="试题编号"
            width="150">
          </el-table-column>
          <el-table-column
            prop="subject"
            label="学科"
            width="120">
          </el-table-column>
          <el-table-column
            prop="catalog"
            label="目录"
            width="120">
          </el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
            width="120">
            <template slot-scope="scope">
              <p v-if="scope.row.questionType === '1'">单选</p>
              <p v-else-if="scope.row.questionType === '2'">多选</p>
              <p v-else>简答</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="question"
            label="题干"
            width="200">
            <template slot-scope="scope">
              <div v-html="scope.row.question"></div>
            </template>
          </el-table-column>
          <el-table-column
            prop="addDate"
            label="录入时间"
            width="180">
            <template slot-scope="scope">
              {{ scope.row.addDate | formatTime }}
            </template>
          </el-table-column>
          <el-table-column
            prop="difficulty"
            label="难度"
            width="120">
            <template slot-scope="scope">
              <p v-if="scope.row.difficulty === '1'">简单</p>
              <p v-else-if="scope.row.difficulty === '2'">一般</p>
              <p v-else>困难</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="creator"
            label="录入人"
            width="120">
          </el-table-column>
          <el-table-column
            prop="chkState"
            label="审核状态"
            width="120">
            <template slot-scope="scope">
              <p v-if="scope.row.chkState === 0">待审核</p>
              <p v-else-if="scope.row.chkState === 1">已审核</p>
              <p v-else>已拒绝</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="chkRemarks"
            label="审核意见"
            width="120">
          </el-table-column>
          <el-table-column
            prop="chkUser"
            label="审核人"
            width="120">
          </el-table-column>
          <el-table-column
            prop="publishState"
            label="发布状态"
            width="120">
            <template slot-scope="scope">
              <p v-if="scope.row.publishState === 1">已发布</p>
              <p v-else-if="scope.row.publishState === 0">已下架</p>
              <p v-else>待发布</p>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="200">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="preview(scope.row)"
              >预览</el-button>
              <el-button
                type="text"
                size="small"
                :disabled="scope.row.chkState !== 0"
                @click="examine(scope.row)"
              >审核</el-button>
              <el-button
                type="text"
                size="small"
                :disabled="scope.row.publishState === 1"
                @click="$router.push('/questions/new?id='+ scope.row.id )"
              >修改</el-button>
              <el-button
                type="text"
                size="small"
                @click="editPublishState(scope.row)"
              >{{scope.row.publishState === 1 ? '下架' : '上架'}}
              </el-button>
              <el-button type="text" size="small" :disabled="scope.row.publishState === 1" @click="delData(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <div class="btn_wrapper">
          <el-pagination
            :current-page="query.page"
            :page-sizes="[2, 5, 10, 15]"
            :page-size="query.pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="total"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>

        <!-- 审核弹出框 -->
        <el-dialog
          title="题目审核"
          v-if="isShowDialog"
          :visible.sync="isShowDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          width="30%">
          <div style="display: block; margin-bottom: 20px">
            <el-radio v-model="chkState" label="1">通过</el-radio>
            <el-radio v-model="chkState" label="2">不通过</el-radio>
          </div>
          <el-input
            style="width: 100%"
            type="textarea"
            autosize
            placeholder="请输入审核内容"
            v-model="chkRemarks">
          </el-input>
          <span slot="footer" class="dialog-footer">
            <el-button @click="isShowDialog = false">取 消</el-button>
            <el-button type="primary" @click="onSubmit">确 定</el-button>
          </span>
        </el-dialog>

        <!-- 预览弹出框 -->
        <el-dialog
          title="题目预览"
          :visible.sync="isShowPreviewDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          v-if="isShowPreviewDialog"
          width="68%">
          <QuestionsDialog :id="id"/>
          <span slot="footer" class="dialog-footer btn_wrapper">
            <el-button type="primary" @click="isShowPreviewDialog = false">关闭</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { choice, choicePublish, remove, choiceCheck } from '@/api/hmmm/questions'
import { list } from '@/api/hmmm/subjects'
import { createAPI } from '@/utils/request'
import { direction, difficulty, questionType } from '@/api/hmmm/constants'
import { provinces, citys } from '@/api/hmmm/citys.js'
import dayjs from 'dayjs'

import QuestionsDialog from './questions-dialog'

export default {
  name: 'QuestionsChoice',
  components: {
    QuestionsDialog
  },
  data () {
    return {
      query: {
        page: 1, // 页码
        pagesize: 2 // 每页条数
      },
      // 精选题库列表
      questionsData: [],
      formData: {
        subjectID: null, // 学科
        questionType: null, // 试题类型
        creatorID: null, // 录入人
        tags: null, // 标签
        chkState: null, // 状态
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        keyword: null, // 关键字
        catalogID: null, // 目录
        difficulty: null, // 难度
        direction: null, // 方向
        province: null, // 城市(省)
        city: null // 城市(市)
      },
      // 学科列表
      subjectList: [],
      // 二级目录列表
      SecondaryDirectorys: [],
      // 标签列表
      tagsList: [],
      // 录入人列表
      usersList: [],
      // 城市
      citySelect: {
        province: [],
        cityDate: []
      },
      activeName: 'first',
      // 总条数
      total: 0,
      isShowDialog: false,
      id: '',
      chkState: '1', // 1 通过 2 不通过
      chkRemarks: '', // 原因
      // 预览
      isShowPreviewDialog: false
    }
  },
  created () {
    // 调用获取精选题库列表方法
    this.choice()
  },
  computed: {
    direction () {
      return direction
    },
    difficulty () {
      return difficulty
    },
    questionType () {
      return questionType
    }
  },
  filters: {
    // 时间过滤器
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD  HH:mm:ss')
    }
  },
  methods: {
    async searchChoice () {
      this.query.page = 1
      const res = await choice({ ...this.query, ...this.formData })
      console.log(res)
      this.questionsData = res.data.items
      this.total = res.data.counts
    },
    // 点击预览
    preview (row) {
      this.id = row.id
      this.isShowPreviewDialog = true
    },

    // 点击弹框确认按钮
    async onSubmit () {
      const data = {
        id: this.id,
        chkState: +this.chkState,
        chkRemarks: this.chkRemarks
      }
      await choiceCheck(data)
      this.choice()
      this.isShowDialog = false
      this.chkRemarks = ''
    },

    // 点击审核
    examine (row) {
      this.id = row.id
      this.isShowDialog = true
    },

    // 点击修改按钮
    editPublishState (row) {
      if (row.publishState === 0) {
        this.$confirm('确认上架吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          row.publishState = 1
          await choicePublish(row)
          this.$message({
            type: 'success',
            message: '上架成功'
          })
        })
      } else {
        this.$confirm('确认下架吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          row.publishState = 0
          await choicePublish(row)
          this.$message({
            type: 'success',
            message: '下架成功'
          })
        })
      }
    },

    // 点击删除按钮
    async delData (row) {
      const confirm = await this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirm === 'cancel') return
      await remove({ id: row.id })
      if (this.questionsData.length === 1) {
        this.query.page--
        if (this.query.page <= 1) {
          this.query.page = 1
        }
      }
      this.choice()
    },

    // 清空按钮
    resetForm () {
      this.formData = {
        subjectID: null, // 学科
        questionType: null, // 试题类型
        creatorID: null, // 录入人
        tags: null, // 标签
        chkState: null, // 状态
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        keyword: null, // 关键字
        catalogID: null, // 目录
        difficulty: null, // 难度
        direction: null, // 方向
        province: null, // 城市(省)
        city: null // 城市(市)
      }
      // 学科列表
      this.subjectList = []
      // 二级目录列表
      this.SecondaryDirectorys = []
      // 标签列表
      this.tagsList = []
      // 录入人列表
      this.usersList = []
      // 城市
      this.citySelect = {
        province: [],
        cityDate: []
      }
      this.$refs.formData.resetFields()
      this.query.page = 1
      this.choice()
    },

    // 选择学科
    changeSubject () {
      this.directoryList()
      this.tagList()
    },

    // 获取省
    getCityData () {
      this.citySelect.province = provinces()
    },

    // 选省获取到市
    handleProvince (e) {
      this.citySelect.cityDate = citys(e)
      // this.requestParameters.city = this.citySelect.cityDate[0]
    },

    // 获取录入人列表
    async userList () {
      const list = data => createAPI('/users', 'get', data)
      const res = await list()
      this.usersList = res.data.list
    },

    // 获取标签列表
    async tagList () {
      const list = data => createAPI('/tags', 'get', data)
      const res = await list({ subjectID: this.formData.subjectID })
      this.tagsList = res.data.items
    },

    // 获取目录列表
    async directoryList () {
      const list = data => createAPI('/directorys', 'get', data)
      const res = await list({ subjectID: this.formData.subjectID })
      this.SecondaryDirectorys = res.data.items
    },

    // 获取学科列表
    async list () {
      const res = await list({ pagesize: 100, ...this.formData })
      this.subjectList = res.data.items
    },

    // 获取精选题库列表
    async choice () {
      const res = await choice(this.query)
      this.questionsData = res.data.items
      this.total = res.data.counts
    },

    // 每页显示的条数发生改变时触发
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.choice()
    },

    // 当前页面发生改变时触发
    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.choice()
    },

    // tabs 标签页
    handleClick (tab, event) {
      if (tab.name === 'null') {
        this.formData.chkState = null
      }
      if (tab.name !== 'null') {
        this.formData.chkState = tab.name
      }
      this.choice()
    }
  }
}
</script>

<style scoped>
.btn_wrapper {
  margin-bottom: 15px;
  display: flex;
  justify-content: flex-end;
}
.el-row {
  margin-bottom: 20px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

::v-deep .el-form-item__label{
  width: 80px;
}
::v-deep .el-form-item__content {
  display: flex
}
::v-deep .el-select{
  width: 100%;
}
</style>
